﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using VPLink2013.Framework.Extensions;
using VPLink2014.Data.MesApplis;

namespace VPLink2014
{
    public partial class ApplicationOperationsByManager : ApplicationOperations
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string responseJson = "{}";
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    if (!string.IsNullOrEmpty(this.Request.QueryString["search"]))
                    {
                        string search = this.Request.QueryString["search"];
                        responseJson = InitFilterDefaultApplications(search);
                    }
                    else
                    {
                        int nbAppli = this.Request.QueryString.AllKeys.Length;

                        if (nbAppli > 0)
                        {
                            if (!string.IsNullOrEmpty(this.Request.QueryString["keyApplication[0][type]"])
                            && !string.IsNullOrEmpty(this.Request.QueryString["keyApplication[0][value]"]))
                            {
                                string teamType = this.Request.QueryString["keyApplication[0][type]"];
                                string teamName = this.Request.QueryString["keyApplication[0][value]"];
                                responseJson = SetApplicationsToTeam(nbAppli / 5, teamType, teamName);
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(this.Request.QueryString["type"])
                                    && !string.IsNullOrEmpty(this.Request.QueryString["value"]))
                                {
                                    string teamType = this.Request.QueryString["type"];
                                    string teamName = this.Request.QueryString["value"];
                                    responseJson = SetApplicationsToTeam(0, teamType, teamName);
                                }
                            }
                        }
                    }
                });
                Response.Write(responseJson);
                Response.End();
            }
            catch (System.Threading.ThreadAbortException)
            {
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
        }
    }
}
